package com.ledim.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimSearchSuggestion;
import com.ledim.itf.LedimOnItemClickListener;
import com.letv.android.young.client.R;

public class SearchSuggestedAdapter extends LedimBaseListAdapter<LedimSearchSuggestion> {

    public void setKeyTemp(String mKeyTemp) {
        this.mKeyTemp = mKeyTemp;
    }

    private String mKeyTemp;
    private LedimOnItemClickListener mOnItemClickListener;

    public SearchSuggestedAdapter(Context context, LedimOnItemClickListener onItemClickListener,
                                  String keyTemp) {
        super(context);
        mKeyTemp = keyTemp;
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_search_suggested, null);
            holder.mName = (TextView) convertView.findViewById(R.id.search_related_item_name);
            holder.mCategory =
                    (TextView) convertView.findViewById(R.id.search_related_item_category);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final LedimSearchSuggestion suggestion = getItem(position);
        int len = mKeyTemp.length();
        if (suggestion.name.startsWith(mKeyTemp)) {
            String styleSource = "<font size=17 color='#FA5F5F'>" + mKeyTemp +
                    "</font><font size=14 color='#333333'>" + suggestion.name.substring(len) +
                    "</font>";
            holder.mName.setText(Html.fromHtml(styleSource));
            holder.mCategory.setText(suggestion.category);
        } else {
            holder.mName.setText(suggestion.name);
            holder.mCategory.setText(suggestion.category);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position, suggestion, v);
                }
            }
        });
        return convertView;
    }

    public static class ViewHolder {

        TextView mName;
        TextView mCategory;
    }
}
